﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AXBC = Microsoft.Dynamics.BusinessConnectorNet;
using AXMD = Microsoft.Dynamics.Framework.Metadata.Ax;
using Proxy = Microsoft.Dynamics.Framework.BusinessConnector.Proxy;
using DynamicsAXManagement.Extensions;

namespace DynamicsAXManagement
{
    public static class Util
    {

        public static string GetBinaryName( ROOT.CIMV2.Win32.Service service)
        {
            string pathname = service.PathName;
            if (pathname.StartsWith("\""))
            {
                int n = pathname.IndexOf( '\"', 1 );
                string s = pathname.Substring(1,n-2);
                return s;

            }
            else
            {
                var tokens = pathname.Split();
                return tokens[0];
            }
        }

        public static List<ReportServerInstance> GetReportServerInstanced()
        {
            var wmi_objects = new List<ROOT.CIMV2.Win32.Service>();
            foreach (ROOT.CIMV2.Win32.Service service in ROOT.CIMV2.Win32.Service.GetInstances())
            {
                wmi_objects.Add(service);
            }

            var rs_instances = new List<ReportServerInstance>();
            foreach (var wmi_object in wmi_objects)
            {
                if (wmi_object.Name.StartsWith("ReportServer"))
                {
                    var rsi = new ReportServerInstance();
                    rsi.WMIObject = wmi_object;
                    rsi.ExecutablePath = GetBinaryName( wmi_object);
                    rs_instances.Add(rsi);
                }

            }

            return rs_instances;

        }



    }
}
